//
//  LHXCutImageView.m
//  Etwo
//
//  Created by 林浩翔 on 16/6/17.
//  Copyright © 2016年 林浩翔. All rights reserved.
//

#import "LHXCutImageView.h"

#define StarsBatHeight 15

@interface LHXCutImageView ()

@property (assign,nonatomic)BOOL isBig;

@end


@implementation LHXCutImageView


+ (instancetype)cutImageWith:(UIImage *)image{
    
    LHXCutImageView *imageV = [[self alloc]initWithImage:image];
    
    imageV.contentMode = UIViewContentModeScaleAspectFill;
    CGFloat Wpower = [UIScreen mainScreen].bounds.size.width / imageV.frame.size.width;
    CGFloat Hpower = [UIScreen mainScreen].bounds.size.height / imageV.frame.size.height;
    
    
    CGRect imageF = imageV.frame;
    if (Wpower >= Hpower){
        imageF.size.width = Hpower * imageF.size.width;
        imageF.size.height = Hpower * imageF.size.height;
        imageV.scale = Hpower;
    }else{
        imageF.size.width = Wpower * imageF.size.width;
        imageF.size.height = Wpower * imageF.size.height;
        imageV.scale = Wpower;
    }
    
    imageV.frame = imageF;
    
    imageV.center = CGPointMake([UIScreen mainScreen].bounds.size.width / 2, [UIScreen mainScreen].bounds.size.height / 2);
    imageV.userInteractionEnabled = YES;
    
    imageV.fristFrame = imageV.frame;
    
    
    imageV.isBig = YES;
    
    
    
//    //添加捏合手势
//    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:imageV action:@selector(pinch:)];
//    
//    pinch.delegate = imageV;
//    
//    imageV.pinch = pinch;
//    
//    [imageV addGestureRecognizer:pinch];
    
    //添加双击手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:imageV action:@selector(doubleClick:)];
    
    tap.numberOfTapsRequired = 2;
    
    imageV.tap = tap;
    
    [imageV addGestureRecognizer:tap];
    
    
    if (imageV.frame.origin.y < StarsBatHeight) {
        [[UIApplication sharedApplication]setValue:@0 forKeyPath:@"statusBar.alpha"];
    }else{
        [[UIApplication sharedApplication]setValue:@1 forKeyPath:@"statusBar.alpha"];
    }
 
    return imageV;

}
#pragma mark --手势任务

- (void)rotation:(UIRotationGestureRecognizer *)rotatin{
    
    self.transform =  CGAffineTransformRotate(self.transform, rotatin.rotation);
    //复位
    [rotatin setRotation:0];
//    NSLog(@"ROTATION  didRotation");
    if (rotatin.state == UIGestureRecognizerStateEnded) {
//        NSLog(@"ROTATION  endRotation");
    }
    
}


//双击缩小放大手势
- (void)doubleClick:(UITapGestureRecognizer *)tap{
    
    if (self.isBig) {
        
        CGRect tempFrame = self.frame;
        
        if (self.frame.size.width < [UIScreen mainScreen].bounds.size.width) {
            
            CGFloat Wscale =  [UIScreen mainScreen].bounds.size.width / self.frame.size.width;
            
            tempFrame.size.width = tempFrame.size.width * Wscale;
            
            tempFrame.size.height = tempFrame.size.height * Wscale;
            
            tempFrame.origin.x = 0;
            
            tempFrame.origin.y = - ((tempFrame.size.height - [UIScreen mainScreen].bounds.size.height) / 2);

        }else if(self.frame.size.height < [UIScreen mainScreen].bounds.size.height){
            
            CGFloat Hscale = [UIScreen mainScreen].bounds.size.height / self.frame.size.height;
            
            tempFrame.size.width = tempFrame.size.width * Hscale;
            
            tempFrame.size.height = tempFrame.size.height * Hscale;
            
            tempFrame.origin.y = 0;
            
            tempFrame.origin.x = - ((tempFrame.size.width - [UIScreen mainScreen].bounds.size.width) / 2);
        }
        [UIView animateWithDuration:0.2 animations:^{
            self.frame = tempFrame;
        }];
        
        
        [[UIApplication sharedApplication]setValue:@0 forKeyPath:@"statusBar.alpha"];

    }else{

        [UIView animateWithDuration:0.2 animations:^{
            self.frame = self.fristFrame;
        }];
        

        [[UIApplication sharedApplication]setValue:@1 forKeyPath:@"statusBar.alpha"];
    }
    self.isBig = !self.isBig;
    
    
}



//捏合手势pinch
- (void)pinch:(UIPinchGestureRecognizer *)pinch {
    
    
    self.transform = CGAffineTransformScale(self.transform, pinch.scale, pinch.scale);
    
    //复位操作
    [pinch setScale:1];
    
    //修改状态栏隐藏属性
    if(self.frame.origin.y < StarsBatHeight){
        [[UIApplication sharedApplication]setValue:@0 forKeyPath:@"statusBar.alpha"];
    }else{
        [[UIApplication sharedApplication]setValue:@1 forKeyPath:@"statusBar.alpha"];
        
    }
    
    if (pinch.state == UIGestureRecognizerStateEnded) {
        
        if ((self.frame.size.width < [UIScreen mainScreen].bounds.size.width) &&(self.frame.size.height < [UIScreen mainScreen].bounds.size.height)){
                self.isBig = YES;
            [UIView animateWithDuration:0.2 animations:^{
                self.frame = self.fristFrame;
            }];
                
        }else if ((self.frame.size.width >= [UIScreen mainScreen].bounds.size.width) &&(self.frame.size.height >= [UIScreen mainScreen].bounds.size.height)){
                        
            self.isBig = NO;
        }else if((self.frame.size.width < [UIScreen mainScreen].bounds.size.width) || (self.frame.size.height < [UIScreen mainScreen].bounds.size.height)){
            
            self.isBig = YES;
        }
        
        
    
    }
    
}


//当手指拖动时会持续调用pan
- (void)pan:(UIPanGestureRecognizer *)pan {
    
    
    //获取相对于最原始的偏移量
    CGPoint transP = [pan translationInView:self];
    //平移
    self.transform = CGAffineTransformTranslate(self.transform, transP.x, transP.y);
    //复位操作(相对于上一次)CGPointMake(0, 0)
    [pan setTranslation:CGPointZero inView:self];
//    NSLog(@"Pan didPan..........");
    //修改状态栏隐藏属性
    if(self.frame.origin.y < StarsBatHeight){
        [[UIApplication sharedApplication]setValue:@0 forKeyPath:@"statusBar.alpha"];
    }else{
        [[UIApplication sharedApplication]setValue:@1 forKeyPath:@"statusBar.alpha"];
        
    }
    
    if (pan.state == UIGestureRecognizerStateEnded) {
        CGRect cutImageF = self.frame;
        [self getBackFrameWithFrame:cutImageF];
    }
}

/**滑动之后假如超出屏幕返回*/
- (void)getBackFrameWithFrame:(CGRect)cutImageF{
    

    if (self.frame.size.height < [UIScreen mainScreen].bounds.size.height && self.frame.size.width == [UIScreen mainScreen].bounds.size.width) {
        //假如图片高度比屏幕小,宽度等于屏幕
        if ((self.frame.origin.y > [UIScreen mainScreen].bounds.size.height * 0.4)||(CGRectGetMaxY(self.frame) < [UIScreen mainScreen].bounds.size.height * 0.6)) {//图片上下部超过屏幕弹回
            CGFloat space = ([UIScreen mainScreen].bounds.size.height - self.frame.size.height) / 2;
            CGFloat Y = [UIScreen mainScreen].bounds.size.height - CGRectGetMaxY(self.frame);
            cutImageF.origin.y = cutImageF.origin.y + Y - space;
        }
        //图片左右始终贴近边框
        if ((self.frame.origin.x > 0)||(CGRectGetMaxX(self.frame) < [UIScreen mainScreen].bounds.size.width)) {
            cutImageF.origin.x = 0;
        }
        
    }else if (self.frame.size.width < [UIScreen mainScreen].bounds.size.width && self.frame.size.height == [UIScreen mainScreen].bounds.size.height){
        //假如图片宽度比屏幕小,高度等于屏幕
        if ((self.frame.origin.x > [UIScreen mainScreen].bounds.size.width * 0.4)||(CGRectGetMaxX(self.frame) < [UIScreen mainScreen].bounds.size.width * 0.6)) {//图片左右部超过屏幕弹回
            CGFloat space = ([UIScreen mainScreen].bounds.size.width - self.frame.size.width) / 2;
            cutImageF.origin.x = space;
        }
        //图片上下始终贴近边框
        if ((self.frame.origin.y > 0 )|| (CGRectGetMaxY(self.frame) < [UIScreen mainScreen].bounds.size.height)) {
            cutImageF.origin.y = 0;
        }
        
    }else if (self.frame.size.width == [UIScreen mainScreen].bounds.size.width && self.frame.size.height == [UIScreen mainScreen].bounds.size.height){
        //图片宽高等于屏幕
        if ((self.frame.origin.y > [UIScreen mainScreen].bounds.size.height * 0.35) || (CGRectGetMaxY(self.frame) < [UIScreen mainScreen].bounds.size.height * 0.65)) {
            cutImageF.origin.y = 0;
        }
        
        if ((self.frame.origin.x > [UIScreen mainScreen].bounds.size.width * 0.35)||(CGRectGetMaxX(self.frame) < [UIScreen mainScreen].bounds.size.width * 0.65)) {
            cutImageF.origin.x = 0;
        }
        
    }else if((self.frame.size.width > [UIScreen mainScreen].bounds.size.width && self.frame.size.height > [UIScreen mainScreen].bounds.size.height)){
        //图片宽高大于屏幕
        if (self.frame.origin.y > [UIScreen mainScreen].bounds.size.height * 0.4) {
            cutImageF.origin.y = 0;
        }else if (CGRectGetMaxY(self.frame) < [UIScreen mainScreen].bounds.size.height * 0.6){
            
            CGFloat Y = [UIScreen mainScreen].bounds.size.height - CGRectGetMaxY(self.frame);
            
            cutImageF.origin.y = cutImageF.origin.y + Y ;
        }
        if (self.frame.origin.x > [UIScreen mainScreen].bounds.size.width * 0.4) {
            cutImageF.origin.x = 0;
        }else if (CGRectGetMaxX(self.frame) < [UIScreen mainScreen].bounds.size.width * 0.6){
            
            CGFloat X = [UIScreen mainScreen].bounds.size.width - CGRectGetMaxX(self.frame);
            
            cutImageF.origin.x = cutImageF.origin.x + X ;
        }
        
    }else if((self.frame.size.width > [UIScreen mainScreen].bounds.size.width && self.frame.size.height <= [UIScreen mainScreen].bounds.size.height)){
        //图片宽度比屏幕大,高度小于屏幕
        if (self.frame.origin.x > 0) {
            cutImageF.origin.x = 0;
        }else if(CGRectGetMaxX(self.frame) < [UIScreen mainScreen].bounds.size.width){
            CGFloat Xspace = [UIScreen mainScreen].bounds.size.width - CGRectGetMaxX(self.frame);
            cutImageF.origin.x = cutImageF.origin.x + Xspace;
        }
        
        if ((self.frame.origin.y > [UIScreen mainScreen].bounds.size.height * 0.4)||(CGRectGetMaxY(self.frame) < [UIScreen mainScreen].bounds.size.height * 0.6)) {
            cutImageF.origin.y = ([UIScreen mainScreen].bounds.size.height - self.frame.size.height) / 2;
        }
        
    }else if((self.frame.size.width <= [UIScreen mainScreen].bounds.size.width && self.frame.size.height > [UIScreen mainScreen].bounds.size.height)){
        //图片宽度比屏幕小,高度大于屏幕
        if (self.frame.origin.y > 0) {
            cutImageF.origin.y = 0;
        }else if(CGRectGetMaxY(self.frame) < [UIScreen mainScreen].bounds.size.height){
            CGFloat Yspace = [UIScreen mainScreen].bounds.size.height - CGRectGetMaxY(self.frame);
            cutImageF.origin.y = cutImageF.origin.y + Yspace;
        }
        
        if ((self.frame.origin.x > [UIScreen mainScreen].bounds.size.width * 0.4)||(CGRectGetMaxX(self.frame) < [UIScreen mainScreen].bounds.size.width * 0.6)) {
            cutImageF.origin.x = ([UIScreen mainScreen].bounds.size.width - self.frame.size.width) / 2;
        }
    }else if ((self.frame.size.width < [UIScreen mainScreen].bounds.size.width) && (self.frame.size.height < [UIScreen mainScreen].bounds.size.height)){
        cutImageF = self.fristFrame;
    }
    
    
    if(cutImageF.origin.y < StarsBatHeight){
        [[UIApplication sharedApplication]setValue:@0 forKeyPath:@"statusBar.alpha"];
    }else{
        [[UIApplication sharedApplication]setValue:@1 forKeyPath:@"statusBar.alpha"];
        
    }
    
    [UIView animateWithDuration:0.2 animations:^{
        self.frame = cutImageF;
    }];
    

}



#pragma mark --是否支持多个手势的代理方法
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return YES;//返回YES即使表示可以同时支持多种手势
}

@end
